package com.jair.tools;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

public class ParasrExcelUtils {
	public static void parser(MultipartFile file){
		try {
			InputStream is = file.getInputStream();
			Workbook wb = null;
			 if (file.getOriginalFilename().contains("xlsx")) {
			        wb = new XSSFWorkbook(is);
			    } else {
			        wb = new HSSFWorkbook(is);
			    }
						for (int k = 0; k < wb.getNumberOfSheets(); k++) {
							Sheet sheet = wb.getSheetAt(k);
							int rows = sheet.getPhysicalNumberOfRows();
							System.out.println("Sheet " + k + " \"" + wb.getSheetName(k) + "\" has " + rows
									+ " row(s).");
							for (int r = 0; r < rows; r++) {
								Row row = sheet.getRow(r);
								if (row == null) {
									continue;
								}

								System.out.println("\nROW " + row.getRowNum() + " has " + row.getPhysicalNumberOfCells() + " cell(s).");
								for (int c = 0; c < row.getLastCellNum(); c++) {
									Cell cell = row.getCell(c);
									String value;

									if (cell != null) {
										switch (cell.getCellType()) {

											case Cell.CELL_TYPE_FORMULA:
												value = "FORMULA value=" + cell.getCellFormula();
												break;

											case Cell.CELL_TYPE_NUMERIC:
												value = "NUMERIC value=" + cell.getNumericCellValue();
												break;

											case Cell.CELL_TYPE_STRING:
												value = "STRING value=" + cell.getStringCellValue();
												break;

											case Cell.CELL_TYPE_BLANK:
												value = "<BLANK>";
												break;

											case Cell.CELL_TYPE_BOOLEAN:
												value = "BOOLEAN value-" + cell.getBooleanCellValue();
												break;

											case Cell.CELL_TYPE_ERROR:
												value = "ERROR value=" + cell.getErrorCellValue();
												break;

											default:
												value = "UNKNOWN value of type " + cell.getCellType();
										}
										System.out.println("CELL col=" + cell.getColumnIndex() + " VALUE="
												+ value);
									}
								}
							}
						}
					
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
